
from rest_framework.views import APIView
from apps.Basic_data.models import Units, Colors, Suppliers, Clients, Goods
from apps.Basic_data.serializers import BasicUnitSerializer, BasicColorSerializer, BasicSupplierSerializer, BasicGoodsSerializer, BasicClientSerializer
from rest_framework.schemas import AutoSchema
from utils import ResponseMessage


# 定义一个基类APIView，用于处理通用的HTTP请求
class BaseAPIView(APIView):
    schema = AutoSchema()

    # 定义一个获取对象的方法，用于获取指定ID的对象
    def get_object(self, model, id):
        try:
            return model.objects.get(id=id)
        except model.DoesNotExist:
            return None

    # 处理HTTP GET请求，用于查询指定ID的对象或所有对象
    def get(self, request, *args, **kwargs):
        id = request.GET.get('id')
        if id:
            obj = self.get_object(self.model, id)
            if obj:
                serializer = self.serializer(obj)
                return ResponseMessage.GoodsResponse.success(serializer.data)
            else:
                return ResponseMessage.GoodsResponse.failed('信息不存在')
        else:
            obj = self.model.objects.all()
            serializer = self.serializer(obj, many=True)
            return ResponseMessage.GoodsResponse.success(serializer.data)

    # 处理HTTP POST请求，用于创建一个新的对象
    def post(self, request, *args, **kwargs):
        serializer = self.serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        obj = serializer.save()
        serializer = self.serializer(obj)
        return ResponseMessage.GoodsResponse.success(serializer.data)

    # 处理HTTP PUT请求，用于更新指定ID的对象
    def put(self, request, *args, **kwargs):
        try:
            obj = self.get_object(self.model, request.data['id'])
            serializer = self.serializer(obj, data=request.data)
            serializer.is_valid(raise_exception=True)
            obj = serializer.save()
            serializer = self.serializer(obj)
            return ResponseMessage.GoodsResponse.success(serializer.data)
        except KeyError:
            return ResponseMessage.GoodsResponse.failed('修改的信息不存在')

    # 处理HTTP DELETE请求，用于删除指定ID的对象
    def delete(self, request, *args, **kwargs):
        try:
            obj = self.get_object(self.model, request.data['id'])
            obj.delete()
            return ResponseMessage.GoodsResponse.success(request.data['id'])
        except KeyError:
            return ResponseMessage.GoodsResponse.failed('删除的信息不存在')


# 定义UnitAPIView类，处理单位信息的增删改查
class UnitAPIView(BaseAPIView):
    model = Units
    serializer = BasicUnitSerializer


# 定义ColorAPIView类，处理颜色信息的增删改查
class ColorAPIView(BaseAPIView):
    model = Colors
    serializer = BasicColorSerializer


# 定义SuppliersAPIView类，处理供应商信息的增删改查
class SuppliersAPIView(BaseAPIView):
    model = Suppliers
    serializer = BasicSupplierSerializer


# 定义GoodsAPIView类，处理商品信息的增删改查
class GoodsAPIView(BaseAPIView):
    model = Goods
    serializer = BasicGoodsSerializer


# 定义ClientAPIView类，处理客户信息的增删改查
class ClientAPIView(BaseAPIView):
    model = Clients
    serializer = BasicClientSerializer




